package com.fs.service.impl;


import com.fs.dao.*;
import com.fs.pojo.ClassPaper;
import com.fs.pojo.Paper;
import com.fs.pojo.Questions;
import com.fs.service.PaperService;
import com.fs.utils.BaseContext;
import com.fs.utils.BeanFactories;
import com.fs.utils.IdWorker;
import com.fs.vo.ClassPaperVO;
import com.fs.vo.CourseClassVO;
import net.sf.cglib.beans.BeanCopier;
import org.apache.commons.lang3.builder.ToStringExclude;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (TbPaper)表服务实现类
 *
 * @author makejava
 * @since 2022-11-15 10:21:17
 */

public class PaperServiceImpl implements PaperService  {

    private ClassPaperDao classPaperDao = BeanFactories.getBean(ClassPaperDao.class);
    private QuestionsDao questionsDao = BeanFactories.getBean(QuestionsDao.class);
    private PaperDao paperDao = BeanFactories.getBean(PaperDao.class);
    private QuestionAnswsersDao answsersDao = BeanFactories.getBean(QuestionAnswsersDao.class);
    private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    private IdWorker idWorker =  new IdWorker();


    @Override
    public List<ClassPaperVO> queryAllPapersByClassIdAndType(String classId, Integer type) {

        //先获取对应试卷集
        List<ClassPaper> classPapers = classPaperDao.queryAllPapersByClassIdAndType(classId, type);

        if(classPapers == null)
            return  null;

        //通过流返回VO对象集
        List<ClassPaperVO> vos = classPapers.stream().map(classPaper -> {
            ClassPaperVO vo = new ClassPaperVO();

            Paper paper = paperDao.queryById(classPaper.getPaperId());

            BeanCopier.create(Paper.class, ClassPaperVO.class, false).copy(paper, vo, null);
            BeanCopier.create(ClassPaper.class, ClassPaperVO.class, false).copy(classPaper, vo, null);

            String userId = BaseContext.getUserId();

            Integer totalScores = answsersDao.getTotalScoreInClassPaperByStuId(classPaper.getId(),userId );
            vo.setTotalScoreOfStudent(totalScores);

            return vo;
        }).collect(Collectors.toList());


        //根据时间排序
        vos.sort((vo1,vo2) ->{
            return  -vo1.getPublishTime().compareTo(vo2.getPublishTime());
        });

        return vos;
    }

    @Override
    public int insertNewPaper(Paper paper, String paperID, List<Questions> questioners) {


        //首先添加paper对象得到生成的paperId
        String createDate = format.format(new Date());


        if(paperID.equalsIgnoreCase("null")){
            //第一次，创建试卷
            String paperId = idWorker.nextId() + "";
            paper.setId(paperId);
            paper.setUpdateTime(createDate);
            paper.setCreateTime(createDate);
            paperDao.insert(paper);

            //开始插入题目数据

            questioners.stream().forEach(questions -> {

                //雪花
                IdWorker questionId =  new IdWorker();
                questions.setId(""+questionId.nextId());
                questions.setPaperId(""+ paperId);

                questionsDao.insert(questions);
            });


        }else {
            questionsDao.deleteAllByPaperId(paperID);

            questioners.stream().forEach(questions -> {
                //雪花
                questions.setId(""+idWorker.nextId());
                questions.setPaperId(""+ paperID);

                if(questions.getAnalysis() == null)
                    questions.setAnalysis("");
            });
            questionsDao.insertList(questioners);
            //不是第一次只用更新修改时间
            paper.setUpdateTime(createDate);
            paper.setId(paperID);
            paperDao.update(paper);
        }





        return 0;
    }

    @Override
    public List<CourseClassVO> qurycourseNameAndClassByTeacherId(String teacherId) {
        ClassDao classDao = BeanFactories.getBean(ClassDao.class);
        List<CourseClassVO>  list =  classDao.qurycourseNameAndClassByTeacherId(teacherId);
        return list;
    }

    @Override
    public List<Paper> queryByTeacherId(String teacherId) {
        List<Paper> papers = paperDao.queryByTeacherId(teacherId);
        return papers;
    }

    @Override
    public int deleteById(String id) {
        return paperDao.deleteById(id);
    }

    @Override
    public int deleteAll(String id) {
        return paperDao.deleteAll(id);
    }


}
